package com.hourse.platform.service.impl;

import com.hourse.platform.vo.MenuVO;
import com.hourse.platform.common.ErrorMsg;
import com.hourse.platform.entity.MenuButtonEntity;
import com.hourse.platform.entity.MenuEntity;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.service.MenuService;
import com.hourse.platform.utils.BeanTools;
import com.hourse.platform.utils.SqlParams;
import com.hourse.platform.utils.StringTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl extends PublicServiceImpl implements MenuService {

  /**
   * @return : com.hourse.platform.vo.PageList
   * @功能描述: 【获取菜单数据】
   * @作 者 :
   * @创建时间 :
   */
  @Transactional(readOnly = true)
  @Override
  public List<MenuVO> getMenuList(String menuId) {

    SqlParams sqlParams = new SqlParams()
      .append("from MenuEntity a where 1 = 1 ");

    if(StringTools.isNotNull(menuId)){
      sqlParams.append(" and a.parentId = :parentId ");
      sqlParams.add("parentId",menuId);
    } else {
      sqlParams.append(" and a.parentId is null");
    }

    sqlParams.append(" order by a.sort");

    List<MenuEntity> tempData = baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());

    List<MenuVO> listData = new ArrayList();

    for(MenuEntity data : tempData){
      MenuVO mvo = (MenuVO) BeanTools.BindEnityUnValidate(data,MenuVO.class);
      mvo.setHasChildren(data.getChildren().size() > 0 ? "true" : "");
      listData.add(mvo);
    }

    return listData;
  }

  /**
   * @功能描述: 【获取菜单按钮】
   * @作 者 :
   * @创建时间 :
   */
  @Transactional(readOnly = true)
  @Override
  public List<MenuButtonEntity> getMenuButtonList(String menuId) {

    SqlParams sqlParams = new SqlParams()
      .append("from MenuButtonEntity a where a.menuId = :menuId")
      .add("menuId",menuId);

    return baseDao.getItemListByNativeHQL(sqlParams.getSql(),sqlParams.getParams());
  }

  /**
   * @功能描述: 【删除菜单按钮】
   */
  @Transactional(rollbackFor = AppException.class)
  @Override
  public void deleteMenuButton(String buttonId) throws AppException {

    try {

      SqlParams sqlParams = new SqlParams();
      sqlParams.append(" delete from menu_button where id= :buttonId");
      sqlParams.add("buttonId",buttonId);
      baseDao.executeSqlUpdate(sqlParams.getSql(),sqlParams.getParams());

      sqlParams.reset();

      sqlParams.append(" delete from role_menu_button where menu_button_id= :buttonId");
      sqlParams.add("buttonId",buttonId);
      baseDao.executeSqlUpdate(sqlParams.getSql(),sqlParams.getParams());

    }catch (Exception e){
      e.printStackTrace();
      ErrorMsg.DB_ERR.Ex();
    }
  }

  /**
   * @功能描述: 【删除菜单】
   */
  @Transactional(rollbackFor = AppException.class)
  @Override
  public void deleteMenu(String menuId) throws AppException {

    try {

      SqlParams sqlParams = new SqlParams();

      sqlParams.append("select count(1) from menu where parent_id = :menuId");
      sqlParams.add("menuId",menuId);

      BigInteger countMenu =
        (BigInteger)baseDao.execSingleQueryBySQL(sqlParams.getSql(),sqlParams.getParams());

      if(countMenu.intValue() > 0){

        throw new AppException("存在子菜单不可删除...");
      }

      sqlParams.reset();

      sqlParams.append(" delete from menu where id= :menuId");
      sqlParams.add("menuId", menuId);
      baseDao.executeSqlUpdate(sqlParams.getSql(), sqlParams.getParams());

      sqlParams.reset();

      sqlParams.append(" delete from menu_button where menu_id= :menuId");
      sqlParams.add("menuId", menuId);
      baseDao.executeSqlUpdate(sqlParams.getSql(), sqlParams.getParams());

      sqlParams.reset();

      sqlParams.append(" delete from role_menu_button where menu_id= :menuId");
      sqlParams.add("menuId", menuId);
      baseDao.executeSqlUpdate(sqlParams.getSql(), sqlParams.getParams());

    } catch (AppException e){
        throw e;
    } catch (Exception e){
      e.printStackTrace();
      ErrorMsg.DB_ERR.Ex();
    }
  }
}
